package com.zhihuichongcheng.application.ui.im.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.smartlib.cmnObject.recycleView.OnItemOnclick;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.bean.mine.GroupContactBean;
import com.zhihuichongcheng.application.bean.mine.UserBean;
import com.zhihuichongcheng.application.glide.GlideCircleTransform;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * Created by 3water on 2016/11/4.
 */
public class ExpListAdapter extends BaseExpandableListAdapter {
    private boolean isShowChildNum = true;
    private OnItemOnclick<UserBean> onItemOnclick;
    private List<String> groups = new ArrayList<>();
    private List<List<GroupContactBean>> childs = new ArrayList<>();
    private Context mContext;
    private UserBean mUserbean;

    public ExpListAdapter(Context context, List<String> groups, List<List<GroupContactBean>> childs) {
        this.childs = childs;
        this.mContext = context;
        this.groups = groups;
    }

    public void setShowChildNum(boolean value) {
        this.isShowChildNum = value;
    }

    public void setOnItemClick(OnItemOnclick<UserBean> onClick) {
        this.onItemOnclick = onClick;
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childs.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childs.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.listadapter_contact_dept, null);
        } else {

        }
        ImageView mImSwitch = (ImageView) convertView.findViewById(R.id.im_switch);
        mImSwitch.setBackgroundResource(R.drawable.ic_contact_close);
        TextView mTvDeptName = (TextView) convertView.findViewById(R.id.tv_dept_name);
        TextView mTvNum = (TextView) convertView.findViewById(R.id.tv_contact_num);
        mTvDeptName.setText(groups.get(groupPosition));
        mTvNum.setText(childs.get(groupPosition).size() + "");
        if (isExpanded) {
            mImSwitch.setBackgroundResource(R.drawable.ic_contact_open);

        } else {
            mImSwitch.setBackgroundResource(R.drawable.ic_contact_close);
        }

        if (isShowChildNum) {
            mTvNum.setVisibility(View.VISIBLE);
        } else {

            mTvNum.setVisibility(View.GONE);
        }

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.listadapter_contact, null);
        } else {

        }
        final GroupContactBean bean = childs.get(groupPosition).get(childPosition);
        final UserBean userBean = bean.getBean();
        TextView mTvName = (TextView) convertView.findViewById(R.id.name);
        ImageView avatar = (ImageView) convertView.findViewById(R.id.avatar);
        final CheckBox mImGroupCheck = (CheckBox) convertView.findViewById(R.id.im_group_check);
        if (mUserbean != null) {
            if (userBean.getEasemobNumber().equals(mUserbean.getEasemobNumber())) {
                bean.setSelect(true);
                childs.get(groupPosition).set(childPosition, bean);
            }
        }
        mTvName.setText(userBean.getUserName());
        Glide.with(mContext).load(userBean.getLogoUrl()).transform(new GlideCircleTransform(mContext)).placeholder(R.drawable.ic_default_avatar).into(avatar);
        if (isShowChildNum) {
            mImGroupCheck.setVisibility(View.GONE);
        } else {
            mImGroupCheck.setVisibility(View.VISIBLE);
            if (bean.isSelect()) {
                mImGroupCheck.setChecked(true);
                mImGroupCheck.setBackgroundResource(R.drawable.ic_contact_group_selected);
            } else {
                mImGroupCheck.setBackgroundResource(R.drawable.ic_contact_group_normal);
                mImGroupCheck.setChecked(false);
            }
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemOnclick != null) {
                    if (!isShowChildNum) {
                        if (!mImGroupCheck.isChecked()) {
                            bean.setSelect(true);
                            childs.get(groupPosition).set(childPosition, bean);
                            mImGroupCheck.setChecked(true);
                            mImGroupCheck.setBackgroundResource(R.drawable.ic_contact_group_selected);
                        } else {
                            bean.setSelect(false);
                            childs.get(groupPosition).set(childPosition, bean);
                            mImGroupCheck.setChecked(false);
                            mImGroupCheck.setBackgroundResource(R.drawable.ic_contact_group_normal);
                        }
                    }
                    onItemOnclick.onClick(userBean, childPosition);
                }
            }
        });

        return convertView;
    }


    public void setSelectUser(UserBean bean) {
        this.mUserbean = bean;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}
